package com.gagakuai.bpm.service.camunda.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gagakuai.bpm.dao.mapper.camunda.WorkFlowInstenceMapper;
import com.gagakuai.bpm.domian.dto.camunda.WorkFlowInstenceDto;
import com.gagakuai.bpm.domian.entity.camuda.WorkFlowInstence;
import com.gagakuai.bpm.domian.query.camunda.WorkflowQuery;
import com.gagakuai.bpm.service.camunda.WorkFlowInstenceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WorkFlowInstenceServiceImpl implements WorkFlowInstenceService {

    @Autowired
    private WorkFlowInstenceMapper workFlowInstenceMapper;


    @Override
    public int deleteByPrimaryKey(String id) {
        return workFlowInstenceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(WorkFlowInstence record) {
        return workFlowInstenceMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(WorkFlowInstence record) {
        return workFlowInstenceMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(WorkFlowInstence record) {
        return workFlowInstenceMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(WorkFlowInstence record) {
        return workFlowInstenceMapper.insertSelective(record);
    }

    @Override
    public WorkFlowInstence selectByPrimaryKey(String id) {
        return workFlowInstenceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(WorkFlowInstence record) {
        return workFlowInstenceMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(WorkFlowInstence record) {
        return workFlowInstenceMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<WorkFlowInstence> list) {
        return workFlowInstenceMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<WorkFlowInstence> list) {
        return workFlowInstenceMapper.batchInsert(list);
    }


    /*
     * @param workflowQueryDto
     * @param userId
     * @return {@link Object}
     * @Author houhong
     * @Description //TODO 获取工作流实列
     * @Date 10:48 上午 2022/3/22
     */
    @Override
    public IPage<WorkFlowInstenceDto> getApplyProcessByUer(WorkflowQuery workflowQueryDto, String userId) throws Exception {


        IPage<WorkFlowInstence> pageDto = new Page<>(workflowQueryDto.getPageNumber(), workflowQueryDto.getPageSize());

        IPage<WorkFlowInstence> workFlowInstenceIPage = workFlowInstenceMapper.selectApplyProcessByUer(pageDto, workflowQueryDto, userId);

        List<WorkFlowInstence> records = workFlowInstenceIPage.getRecords();

        List<WorkFlowInstenceDto> result = new ArrayList<>();

        records.stream().forEach(workFlowInstence -> {

            WorkFlowInstenceDto dto = new WorkFlowInstenceDto();

            BeanUtils.copyProperties(workFlowInstence, dto);
            result.add(dto);
        });

        IPage<WorkFlowInstenceDto> resultPage = new Page<>();

        BeanUtils.copyProperties(workFlowInstenceIPage, resultPage);
        resultPage.setRecords(result);

        return resultPage;
    }

}

